package com.probiz.estoresa.content.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.content.NewsletterGroup;
import com.probiz.estore.content.service.NewsletterGroupManager;
import com.probiz.estore.core.controller.GenericStoreAdminController;

public class NewsletterGroupController extends GenericStoreAdminController<NewsletterGroup> {
    private NewsletterGroupManager newsletterGroupManager = null;

    public void setNewsletterGroupManager(NewsletterGroupManager inMgr) {
        this.newsletterGroupManager = inMgr;
    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(NewsletterGroup entity) {
		return entity.getName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = newsletterGroupManager;
	}

	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("1", "convert_gbk(name)");
		sortMap.put("3", "status");
		sortMap.put("4", "isVisibleToFront*-1");
		sortMap.put("5", "sortOrder");
		sortMap.put("6", "updateTime");
		return sortMap;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#preSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, NewsletterGroup entity, BindException errors) {
		if(entity.getStatus()==null)
			entity.setStatus(Constants.STATUS_INACTIVE);
		if(entity.getIsVisibleToFront()==null)
			entity.setIsVisibleToFront(Constants.FLAG_FALSE);
	}
	
	@Override
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		NewsletterGroup entity = formBackingObject(request);
		request.setAttribute("newsletterGroup", entity);
		
		entity.setIsVisibleToFront(Constants.FLAG_TRUE);
		entity.setStatus(Constants.STATUS_ACTIVE);
		
		return new ModelAndView(formView);
	}

}
